package com.yum.ms.utils;

import com.yum.ms.entity.User;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Yum
 * @version 1.0
 */
public class UserUtils {
    public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        createUser(5000);
    }
    private static void createUser(int count) throws SQLException, ClassNotFoundException, IOException {
        List<User> userList = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            User user = new User();
            user.setId(4L+i);
            user.setName("炜神"+i);
            user.setNickName("javaP10工程师");
            userList.add(user);
        }
        // 连接
        Connection connection = getC();
        // sql
        String sql = "insert into t_user(id,name,nick_name) values(?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);

        BufferedWriter out = new BufferedWriter(new FileWriter("D:\\config.txt"));
        for (int i = 0; i < userList.size(); i++) {
            User user = userList.get(i);
            ps.setLong(1,user.getId());
            ps.setString(2, user.getName());
            ps.setString(3, user.getNickName());
            ps.addBatch();
            out.write(user.getId()+","+user.getName()+","+user.getNickName()+"\n");
        }
        ps.executeBatch();
        ps.clearParameters();
        connection.close();
        out.close();
        System.err.println("创建完成");
    }
    public static Connection getC() throws ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://ip:端口/redis_ms?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8";
        String username = "";
        String password = "";
        String driver = "com.mysql.cj.jdbc.Driver";
        Class.forName(driver);
        return DriverManager.getConnection(url,username,password);
    }

}
